﻿using UnityEditor;
using System.IO;
using System.Text;
using UnityEngine;
//编辑器扩展脚本
public class EditorExpand
{
    //得到当前选中的路径及文件夹名称
    private static void GetDirNameAndPath(ref string dirName,ref string path)
    {
        Object objPath = Selection.activeObject;
        path = AssetDatabase.GetAssetPath(objPath);
        if (path == null)
        {
            Log.LogError("文件路径不存在，请检查！");
            return;
        }
        string[] tempPath = path.Split('/');
        dirName= tempPath[tempPath.Length - 1];
    }
    //创建单个lua脚本
    [MenuItem("Assets/Create/LuaScript",false,55)]
    static void CreateLuaScript()
    {
        string dirName = null;
        string path = null;
        GetDirNameAndPath(ref dirName,ref path);
        path = path + "/" + dirName + ".txt";
        string tempPath = Application.dataPath + "/TempTxt/TempLuaScript.txt";
        byte[] luaScript = File.ReadAllBytes(tempPath);
        FileStream fileStream= File.Create(path);
        fileStream.Write(luaScript,0,luaScript.Length);
        fileStream.Close();
        AssetDatabase.Refresh();
    }
    //创建一个lua-MVC
    [MenuItem("Assets/Create/LuaMVC", false, 54)]
    static void CreateLuaMVC()
    {
        string dirName = null;
        string path = null;
        GetDirNameAndPath(ref dirName, ref path);
        for (int i = 0; i<3; i++)
        {
            CreateScript(path, dirName,i);
        }
        AssetDatabase.Refresh();
    }
    //写入脚本内容
    static void CreateScript(string path, string dirName,int type)
    {
        path = path + "/" + dirName + (type == 1 ? "_model.txt" : (type == 2 ? "_view.txt" : "_controller.txt"));
        string readPath = Application.dataPath + "/TempTxt/" + (type == 1 ? "TempModel.txt" : (type == 2 ? "TempView.txt" : "TempController.txt"));
        byte[] luaScript = File.ReadAllBytes(readPath);
        FileStream fileStream = File.Create(path);
        fileStream.Write(luaScript, 0,luaScript.Length);
        fileStream.Close();
    }
}
